//
//  MoreLoanTypeCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/30.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol MoreLoanTypeCellDelegate {
    @objc optional func changeTableViewFrame(height:CGFloat) //修改坐标
    @objc optional func selectLoanDetails(title:String,text:String,index:Int) //选择贷款详情
}

class MoreLoanTypeCell: UITableViewCell {
    
    weak var delegate : MoreLoanTypeCellDelegate?
    
    fileprivate let cellId = "moreLoanTypeCell"
    
    var selectIndex = 0 //当前选中标题
    
    var title : String! {
        didSet{
            titleLabel.text = title
        }
    }
    
    var dataArray = [String]() {
        didSet{
            collectionView.reloadData()
        }
    }
    
    fileprivate let titleLabel = UILabel()
    var collectionView : UICollectionView!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.clear
        self.backgroundColor = UIColor.clear
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        titleLabel.textColor = UIColor.gray
        self.contentView.addSubview(titleLabel)
        
        let layout = LeftEqualFlowLayout()
        layout.minimumLineSpacing = RATIO_H(maxNum: 10)
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.contentView.addSubview(collectionView)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 5))
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    

}

extension MoreLoanTypeCell : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.titleLabel.textColor = UIColor.darkGray
        cell?.contentView.layer.borderWidth = 1
        cell?.contentView.layer.borderColor = UIColor.lineColor().cgColor
        cell?.contentView.layer.cornerRadius = 5
        cell?.contentView.layer.masksToBounds = true
        
        if indexPath.row+1 == dataArray.count {
            self.delegate?.changeTableViewFrame!(height: self.collectionView.contentSize.height)
        }
        
        if indexPath.row == selectIndex {
            cell?.titleLabel.backgroundColor = UIColor.red
            cell?.titleLabel.textColor = UIColor.white
        }else{
            cell?.titleLabel.backgroundColor = UIColor.clear
            cell?.titleLabel.textColor = UIColor.darkGray
        }
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.row != selectIndex {
            let selectCell = collectionView.cellForItem(at: NSIndexPath.init(row: selectIndex, section: 0) as IndexPath) as? TypeCell
            selectCell?.titleLabel.backgroundColor = UIColor.clear
            selectCell?.titleLabel.textColor = UIColor.darkGray
        }else{
            return
        }
        
        self.selectIndex = indexPath.row
        self.delegate?.selectLoanDetails!(title: title, text: dataArray[indexPath.row], index: selectIndex)
        
        let cell = collectionView.cellForItem(at: indexPath) as? TypeCell
        cell?.titleLabel.backgroundColor = UIColor.red
        cell?.titleLabel.textColor = UIColor.white
        
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath) as? TypeCell
        cell?.titleLabel.backgroundColor = UIColor.clear
        cell?.titleLabel.textColor = UIColor.darkGray
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let width = dataArray[indexPath.row].size(font: RATIO(maxNum: 16), height: RATIO_H(maxNum: 30)).width
        
        return CGSize.init(width: width, height: RATIO_H(maxNum: 30))
    }
}
